package beautiful.butterfly.drds.data_exchange.util;


import java.sql.*;
import java.util.Properties;
import java.util.concurrent.Callable;

public class Jdbc
{
    public static void close(ResultSet resultSet, Statement statement, Connection connection)
    {
        if (null != resultSet)
        {
            try
            {
                resultSet.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }

        if (null != statement)
        {
            try
            {
                statement.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }

        if (null != connection)
        {
            try
            {
                connection.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void close(Statement statement, Connection connection)
    {
        close(null, statement, connection);
    }

    public static void close(ResultSet resultSet)
    {
        try
        {
            if (null != resultSet)
            {
                resultSet.close();
            }
            resultSet = null;
        } catch (SQLException e)
        {
            throw new IllegalStateException(e);
        }
    }

    public static void execute(Statement statement, String sql)
            throws SQLException
    {
        statement.execute(sql);
    }

    public static ResultSet executeQuery(Statement statement, String sql)
            throws SQLException
    {
        return statement.executeQuery(sql);
    }

    public static ResultSet executeQuery(Connection connection, String sql, int fetchSize, int queryTimeout)
            throws SQLException
    {

        connection.setAutoCommit(false);
        Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        statement.setFetchSize(fetchSize);
        statement.setQueryTimeout(queryTimeout);
        return executeQuery(statement, sql);
    }

    public static ResultSet executeQuery(Connection connection, String sql, int fetchSize)
            throws SQLException
    {
        // 默认3600 s 的query Timeout
        return executeQuery(connection, sql, fetchSize, Constant.SOCKET_TIMEOUT_INSECOND);
    }

    static synchronized Connection getConnection(String url, Properties properties)
    {
        try
        {
            return DriverManager.getConnection(url, properties);
        } catch (Exception e)
        {
            throw RdbmsException.asConnException(e, properties.getProperty("user"), null);
        }
    }

    //
    public static synchronized Connection getConnection(String url, String username, String password)
    {
        Properties properties = new Properties();
        properties.put("user", username);
        properties.put("password", password);
        return getConnection(url, properties);
    }

    public static Connection getConnectionWithRetry(final String jdbcUrl, final String username, final String password) throws SQLException
    {
        return RetryableTasks.executeWithRetry(new Callable<Connection>()
        {
            public Connection call() throws Exception
            {
                return getConnection(jdbcUrl, username, password);
            }
        }, 9, 1000L, true);
    }

    public static ResultSet executeQuery(Connection connection, String sql)
            throws SQLException
    {
        Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        //默认3600 seconds
        statement.setQueryTimeout(Constant.SOCKET_TIMEOUT_INSECOND);
        return executeQuery(statement, sql);
    }
    //
}
